package user

import (
	"github.com/bgmp/config"
	"github.com/bgmp/handler"
	"github.com/bgmp/model"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
)

func (b Base) UpdateBg(c *gin.Context, req *UpdateBgRequest) *UpdateBgResponse {
	bg := &model.Bg{}
	bg.ID = req.Id
	// 查找出来
	err := bg.Get(config.DB)
	if err != nil {
		handler.Errorf(c, "记录不存在")
		return nil
	}
	// 按照传过来的条件进行修改
	if req.Time.Unix() != 0 {
		bg.Time = req.Time.Time
	}
	if req.Value != 0 {
		bg.Value = req.Value
	}
	if req.Type != model.BgNull {
		bg.Type = req.Type
	}
	// 修改完成后保存
	err = bg.Save(config.DB)
	if err != nil {
		handler.Errorf(c, "更新记录失败")
		log.Error(err)
		return nil
	}
	return &UpdateBgResponse{}
}

type UpdateBgRequest struct {
	Id    uint         `json:"id"`
	Time  model.Time   `json:"time"`
	Value float64      `json:"value"`
	Type  model.BgType `json:"type"`
}

type UpdateBgResponse struct {
}
